import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/filter_components/mixin/advanced_filter_input_mixin.dart';
import 'package:x3_form_core/models/base_advanced_filter_model.dart';

/// author：william on 2021/4/9 14:11
/// email：362661044@qq.com
class AdvancedFilterInputTextRange extends StatefulWidget {
  final BaseAdvancedFilterModel model;

  /// 键盘类型
  final TextInputType keyboardType;

  const AdvancedFilterInputTextRange({Key key, @required this.model, this.keyboardType}) : super(key: key);

  @override
  _AdvancedFilterInputTextRangeState createState() => _AdvancedFilterInputTextRangeState();
}

class _AdvancedFilterInputTextRangeState extends State<AdvancedFilterInputTextRange> with AdvancedFilterInputMixin {
  /// 范围值
  String _startValue = '';
  String _endValue = '';

  @override
  BaseAdvancedFilterModel get model => widget.model;

  @override
  void initStateEvent() {
    var data = baseRequestFormProvider.getDataKey(model);
    if (data != null) {
      if (data == -2) {
        isAll = true;
      }
      if (data is List) {
        _startValue = data.first;
        _endValue = data.last;
      }
    }
  }

  @override
  Widget buildContent() {
    return Row(
      children: [
        Expanded(child: buildTextContainer(_startValue)),
        Container(padding: EdgeInsets.symmetric(horizontal: 8), child: Text('~', style: Theme.of(context).textTheme.subtitle2)),
        Expanded(child: buildTextContainer(_endValue)),
      ],
    );
  }

  /// 编辑弹出框

  Widget _buildShowInputRangeDialog() {
    return Row(
      children: [
        Expanded(
          child: BaseTextField(
            keyboardType: widget.keyboardType,
            inputBorder: inputBorder,
            value: _startValue,
            onChanged: (value) => {_startValue = value},
          ),
        ),
        Container(padding: EdgeInsets.symmetric(horizontal: 8), child: Text('~')),
        Expanded(
          child: BaseTextField(
            keyboardType: widget.keyboardType,
            inputBorder: inputBorder,
            value: _endValue,
            onChanged: (value) => {_endValue = value},
          ),
        ),
      ],
    );
  }

  /// 编辑 文本范围事件
  @override
  void showDialog() {
    String title = '请输入${model.name}范围';
    showDialogEvent(
      title: title,
      child: _buildShowInputRangeDialog(),
      onOKPress: () {
        if (_startValue.isEmpty || _endValue.isEmpty) return BaseUtils.showToast('${model.name}不能为空');
        isAll = false;
        baseRequestFormProvider.setDataSource(model, [_startValue, _endValue]);
        setState(() {});
        Navigator.pop(context);
      },
    );
  }

  @override
  resetEvent() {
    isAll = false;
    _startValue = '';
    _endValue = '';
  }
}
